<?php
/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：文章控制器。
 *
 **/

namespace Qwadmin\Controller;

use Vendor\Tree;

class NovelClassController extends ComController
{
	public function add()
	{
		
		//获得小说的基本分类
		$class_list = D("novel_class")->where("is_other = 0")->select();
		$this->assign("class_list",$class_list);
		//获得小说的其他分类
		$other_class_list = D("novel_class")->where("is_other = 1")->select();
		$this->assign("other_class_list",$other_class_list);
		
		$this->display('form');
	}
	
	public function index($sid = 0, $p = 1)
	{
		$p = intval($p) > 0 ? $p : 1;		
		
		//查询条件
		if(isset($_GET["is_other"]) && $_GET["is_other"] != -1) {
			$map["is_other"] = $_GET["is_other"];
			$this->assign("is_other",$_GET["is_other"]);
		}
		
		$class = M('novel_class');
		$pagesize = 20;#每页数量
		$offset = $pagesize * ($p - 1);//计算记录偏移量
		
		
		$count = $class->where($map)->count();
		$list = $class->where($map)->order("class_id desc")->limit($offset . ',' . $pagesize)->select();
		
		$page = new \Think\Page($count, $pagesize);
		$page = $page->show();
		$this->assign('list', $list);
		$this->assign('page', $page);
		$this->display();
	}
	
	public function del()
	{		
		$class_id = isset($_REQUEST['class_id']) ? $_REQUEST['class_id'] : false;
		if ($class_id) {
			if (is_array($class_id)) {
				$class_id = implode(',', $class_id);
				$map['class_id'] = array('in', $class_id);
			} else {
				$map = 'class_id=' . $class_id;
			}
			if (M('novel_class')->where($map)->delete()) {
				addlog('删除分类，CLASSID：' . $class_id);
				$this->success('恭喜，分类删除成功！');
			} else {
				$this->error('参数错误！');
			}
		} else {
			$this->error('参数错误！');
		}
		
	}
	
	public function edit($class_id)
	{
		$class_id = intval($class_id);
		$class = M('novel_class')->where('class_id=' . $class_id)->find();
		if ($class) {
			$this->assign('class', $class);
			//获得小说的基本分类
			$class_list = D("novel_class")->where("is_other = 0")->select();
			$this->assign("class_list",$class_list);
			//获得小说的其他分类
			$other_class_list = D("novel_class")->where("is_other = 1")->select();
			$this->assign("other_class_list",$other_class_list);
		} else {
			$this->error('参数错误！');
		}
		$this->display('form');
	}
	
	public function update($class_id= 0)
	{
		$class_id= intval($class_id);
		$data['class_name'] = isset($_POST['class_name']) ? intval($_POST['class_name']) : '';
		$data['is_other'] = isset($_POST['is_other']) ? $_POST['is_other'] : 0;
		if (!$data['class_name']) {
			$this->error('警告！分类名必填项目。');
		}
		if ($class_id) {
			M('novel_class')->data($data)->where('class_id=' . $class_id)->save();
			addlog('编辑分类，class_id：' . $class_id);
			$this->success('恭喜！分类编辑成功！', "index.html");
		} else {
			$aid = M('novel_class')->data($data)->add();
			if ($aid) {
				addlog('新增分类，class_id：' . $class_id);
				$this->success('恭喜！分类新增成功！', "index.html");
			} else {
				$this->error('抱歉，未知错误！');
			}
			
		}
	}
}